package com.yuhong.reggie.controller;

import com.yuhong.reggie.common.R;
import com.yuhong.reggie.dto.DishDto;
import com.yuhong.reggie.entity.Page;
import com.yuhong.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired(required = false)
    private DishService dishService;

    //新增套餐-显示指定类别的菜品列表数据
    @RequestMapping("/list")
    public R<List<DishDto>> findByCaregoryId(Long categoryId,Integer status){
        List<DishDto> dishDtoList = dishService.findByCategoryId(categoryId,status);
        return R.success(dishDtoList);
    }

    //修改数据回显
    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable Long id){
        DishDto dishDto = dishService.findById(id);

        return R.success(dishDto);
    }

    @GetMapping("/page")
    public R<Page<DishDto>>  findByPage(Integer page,Integer pageSize,String name){
        Page<DishDto> dtoPage = dishService.findByPage(page, pageSize, name);

        return R.success(dtoPage);
    }

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto, HttpSession session){
        //获取登录人
        Long id = (Long) session.getAttribute("employee");
        //完善修改人,创建人
        dishDto.setUpdateUser(id);
        dishDto.setCreateUser(id);

        dishService.save(dishDto);

        return R.success("成功");
    }
}
